//
//  MALightSubCollectionViewCell.m
//  LoveGame
//
//  Created by 茶 on 2023/10/10.
//

#import "CASavedCollectionViewCell.h"

@interface CASavedCollectionViewCell ()

@property (nonatomic, strong) UIImageView *coverImageView;
@property (nonatomic, strong) UIButton *selectedView;
@end

@implementation CASavedCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.coverImageView];
        [_coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        _selectedView = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectedView setImage:[UIImage imageNamed:@"Saved_0"] forState:UIControlStateNormal];
        [_selectedView setImage:[UIImage imageNamed:@"Saved_1"] forState:UIControlStateSelected];
        [self.contentView addSubview:_selectedView];
        [_selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.ca_offset(5);
            make.right.ca_offset(-5);
        }];
    }
    return self;
}

- (void)setModel:(CASavedModel *)model {
    _model = model;
    _coverImageView.image = [UIImage imageWithContentsOfFile:model.imgPath];
    if (model.canEdit) {
        _selectedView.hidden = NO;
    } else {
        _selectedView.hidden = YES;
    }
    _selectedView.selected = model.isSelected;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    _selectedView.selected = selected;
    _model.isSelected = selected;
}

- (UIImageView *)coverImageView {
    if (!_coverImageView) {
        _coverImageView = [[UIImageView alloc] init];
        _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
        _coverImageView.clipsToBounds = YES;
        _coverImageView.layer.cornerRadius = CARatioWidth(10);
    }
    return _coverImageView;
}

@end
